package com.ghy.controller;

import com.ghy.pojo.Headline;
import com.ghy.service.HeadlineService;
import com.ghy.utils.JwtHelper;
import com.ghy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("headline")
public class HeadlineController {
     @Autowired
     private JwtHelper jwtHelper;

     @Autowired
     private HeadlineService headlineService;

    /**
     * - 用户在客户端输入发布的新闻信息完毕后
     * - 发布前先请求后端的登录校验接口验证登录
     * - 登录通过则提交新闻信息
     * - 后端将新闻信息存入数据库
     * 添加数据
     * @param headline
     * @param token
     * @return
     */
    @PostMapping("publish")
    public Result publish(@RequestBody Headline headline, @RequestHeader String token){
        int userId = jwtHelper.getUserId(token).intValue();
        headline.setPublisher(userId);
        Result result =  headlineService.insertdata(headline);
        return result;
    }

    /**
     * 回显数据
     * @param hid
     * @return
     */
    @PostMapping("findHeadlineByHid")
    public Result findHeadlineByHid(Integer hid){
        Result result = headlineService.findHeadlineByHid(hid);
        return result;
    }

    @PostMapping("update")
    public Result updateData(@RequestBody Headline headline){
        Result result = headlineService.updateData(headline);
        return  result;
    }

    @PostMapping("removeByHid")
    public Result removeByHid(Integer hid){
        Result result = headlineService.removeByHid(hid);
        return result;
    }


}
